package com.ControlEscolar.DataBase;



import com.ControlEscolar.Configuration.ConfigurationLoader;
import com.ControlEscolar.Configuration.DbConfiguration;
import com.ControlEscolar.ORM.ObjectoRelacionGenerador;
import java.sql.*;
import java.util.ArrayList;

public class DbConnection {

        private static DbConnection instance=null;
        private Connection connection;
        private ConfigurationLoader configLoader;
    	private Statement stmt;
        protected ObjectoRelacionGenerador ojbRelGenerator = new ObjectoRelacionGenerador();

        private DbConnection(){
            configLoader = new ConfigurationLoader("setting.conf");
        }

        public static DbConnection initConnection(){
            if(instance == null){
            instance = new DbConnection();
            return instance;
            }else
            return instance;
            

        }

	private void openConection(DbConfiguration dbConfig) throws Exception {
		try {
                    
			Class.forName(dbConfig.getDriver());
                      
            this.setConnection(DriverManager.getConnection(dbConfig.getURL(), dbConfig.getDbUser(), dbConfig.getDbPass()));
		} catch (ClassNotFoundException e) {
                    e.printStackTrace();
			throw new DataBaseException();

		} catch (SQLException e) {
			throw new DataBaseException();
		}
	}

	public void closeConection() throws Exception {
		try {
			if (getaStmt() != null) {

				getaStmt().close();
			}
			if (getConnection() != null) {

				getConnection().close();
			}
		} catch (Exception e) {
			throw new DataBaseException();
		}
	}

        public ResultSet executeViews(String query) throws Exception{
        DbConfiguration currentDbConfig=getConfigLoader().loadConfig();
                        //System.out.println(currentDbConfig.getURL());
                       // System.out.println(query);
                        openConection(currentDbConfig);
                    	setaStmt(getConnection().createStatement());

                        return  getaStmt().executeQuery(query);

        }

	public ArrayList executeQuery(String query, Class c) throws Exception {

        try {
                        DbConfiguration currentDbConfig=getConfigLoader().loadConfig();
                        //System.out.println(currentDbConfig.getURL());
                       // System.out.println(query);
                        openConection(currentDbConfig);
                    	setaStmt(getConnection().createStatement());
                        
                        ResultSet rs = getaStmt().executeQuery(query);
                        
                        return ojbRelGenerator.generaObjetos(rs, c);

		} catch (SQLException e) {
			if (getaStmt() != null) {

				getaStmt().close();
			}
                        e.printStackTrace();
			throw new DataBaseException();
		}
                finally{
                    closeConection();
                }
	}

        public void executeUpdate(String query) throws Exception {
		try {
                 //  System.out.println(query);
                        DbConfiguration currentDbConfig=getConfigLoader().loadConfig();
                        openConection(currentDbConfig);
			setaStmt(getConnection().createStatement());
			getaStmt().executeUpdate(query);
		} catch (SQLException e) {
                    e.printStackTrace();
			if (getaStmt() != null) {

				getaStmt().close();
			}
			throw new DataBaseException();
		}
                finally{
                    closeConection();
                }
	}


    /**
     * @return the connection
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * @param connection the connection to set
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    /**
     * @return the aStmt
     */
    private Statement getaStmt() {
        return stmt;
    }

    /**
     * @param aStmt the aStmt to set
     */
    private void setaStmt(Statement aStmt) {
        this.stmt = aStmt;
    }

    /**
     * @return the configLoader
     */
    private ConfigurationLoader getConfigLoader() {
        return configLoader;
    }




}
